import { SHOW_PATH_INPUT_BAR, HIDE_PATH_INPUT_BAR, UPDATE_PATH_INPUT_BAR } from '../reducers/path-input-bar.js';
import { CREATE_DIR, CREATE_FILE, DELETE_PATH,
    MOVE_PATH } from '../reducers/files.js';
import { createDir, createFile, movePath, openFile } from './files-actions.js';
import { loadDirectory } from './directory-actions.js';

/**
 * 输入完成后可用后续操作
 * @type {Object}
 */
export const NextActionOptions = {
    CREATE_DIR,
    CREATE_FILE,
    MOVE_PATH
};

/**
 * 显示路径输入框
 * @param  {string}            label
 * @param  {NextActionOptions} nextAction
 * @param  {Object}            meta
 * @return {Object}
 */
export function showPathInputBar(label, nextAction, meta = {}) {
    return {
        type: SHOW_PATH_INPUT_BAR,
        payload: {
            label,
            nextAction,
            meta
        }
    };
}

/**
 * 关闭路径输入框
 * @param  {bool}    triggerNextAction
 */
export function hidePathInputBar(triggerNextAction = false) {
    return (dispatch, getState) => {
        // 关闭输入框
        dispatch({
            type: HIDE_PATH_INPUT_BAR
        });

        // 关闭输入框后是否继续执行其他 Action
        if (triggerNextAction) {
            let { nextAction, meta, value } = getState().pathInputBar;

            switch (nextAction) {
                // 创建目录
                case CREATE_DIR:
                    if (value !== meta.origin) {
                        dispatch(createDir(value))
                            .then(() => dispatch(loadDirectory()));
                    }
                    break;
                // 创建文件
                case CREATE_FILE:
                    if (value !== meta.origin) {
                        dispatch(createFile(value))
                            .then(() => dispatch(loadDirectory()))
                            .then(() => dispatch(openFile(value)));
                    }
                    break;
                // 移动文件 (重命名)
                case MOVE_PATH:
                    if (value !== meta.origin) {
                        dispatch(movePath(meta.origin, value))
                            .then(() => dispatch(loadDirectory()));
                    }
                    break;
                default:

            }
        }
    };
}

/**
 * 更新路径输入框(内容)
 * @param  {string}  value
 * @return {Object}
 */
export function updatePathInputBar(value) {
    return {
        type: UPDATE_PATH_INPUT_BAR,
        payload: value
    };
}

const dirArr = [];
let filterArr = [];
let dirIndex = 0;

/**
 * 获取目录列表
 * @param  {Object} node
 * @param  {String} prefix
 */
function getDirs(node, prefix = '') {
    const path = `${prefix}/${node.name}`;

    if (node.type === 'dir') {
        dirArr.push(path);
        node.children.map(child => 
                getDirs(child, path));
    }
}


/**
 * 按Tab键选择目录
 * @return {Object}
 */
export function selectDirectory(value) {
    return (dispatch, getState) => {
        const dirData = getState().directory.tree;

        if(!dirArr.length) {
            getDirs(dirData);
        }

        // filter
        if (!filterArr.length) {
            const reg = new RegExp(value);
            filterArr = dirArr.filter(item => {
                return reg.test(item);
            });
        }
        
        dirIndex++;
        if (dirIndex === filterArr.length){
            dirIndex = 0;
        }

        dispatch({
            type: UPDATE_PATH_INPUT_BAR,
            payload: filterArr[dirIndex].replace(/\/swagger/,'')
        });
    }
}